package Hxw3DEngine.asset.loader
{
	import Hxw3DEngine.asset.AssetType;
	import Hxw3DEngine.error.ErrorType;
	import Hxw3DEngine.asset.AssetManage;
	import Hxw3DEngine.utils.AssetUtils;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	/**
	 *基础加载器，加载完成后自动销毁，一次性的加载器，简单方便就是任性 
	 * @author huangxw
	 */	
	public class AssetLoader extends EventDispatcher
	{
		static public var url_prefix:String;
		
		
		public var url:String = "";
		
		public var type:String = AssetType.RESOURCE_DISPLAY;
		
		private var loader:Loader;
		
		private var urlLoader:URLLoader;
		
		public var callBack:Function;
		
		public var data:*;
		
		/**
		 * 
		 * @param _url
		 * @param _type
		 * @param _callBack   回调没有任何参数，所有加完的资源在加载完成后都已放到资源库中
		 */		
		public function load(_url:String = "",_callBack:Function = null):void{
			if(AssetManage.getAsset(_url)){
				if(Boolean(_callBack)) _callBack();
				return ;
			}
			url = _url; callBack = _callBack; type = AssetUtils.getAssetType(url);
			if(type == AssetType.RESOURCE_DISPLAY){
				loadAsset();
			}else if(type == AssetType.RESOURCE_TXT){
				loadTxt();
			}else if(type == AssetType.RESOURCE_BYTEARRAY){
				loadByteArray();
			}
		}
		
		private function loadAsset():void{
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
			var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			loader.load(new URLRequest(url_prefix + url),context);
		}
		
		private function loadByteArray():void{
			urlLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE,onLoadComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,onError);
			urlLoader.load(new URLRequest(url_prefix + url));
		}
		
		private function loadTxt():void{
			urlLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE,onLoadComplete);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,onError);
			urlLoader.load(new URLRequest(url_prefix + url));
		}
		
		private function onError(e:IOErrorEvent):void{
			throw new Error(ErrorType.RESOURCE_LOAD_ERROR + url_prefix + url);
		}
		
		private function onLoadComplete(e:Event):void{
			if(e.target is URLLoader){
				data = e.target.data;
			}else if(e.target is LoaderInfo){
				data = e.target.content.bitmapData;
			}
			AssetManage.addAsset(url,data);
			if(Boolean(callBack)) callBack();			
			dispose();
		}
		
		
		public function dispose():void{
			if(loader){
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoadComplete);
				loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				loader = null
			}
			
			if(urlLoader){
				urlLoader.removeEventListener(Event.COMPLETE,onLoadComplete);
				urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				loader = null
			}
			callBack = null ;
			
		}
		
		
	}
}